let image = $('#image')
let option = {
  aspectRatio: 1,
  preview: '.img-preview'
}
image.cropper(option)

$('button:contains("上传")').on('click', function () {
  $('#file').trigger('click')
})

$('#file').on('change', function () {
  if (this.files.length > 0) {
    let fileObj = this.files[0]
    let url = URL.createObjectURL(fileObj)
    image.cropper('replace', url)
  }
})

$('button:contains("确定")').on('click', function () {
  let canvas = image.cropper('getCroppedCanvas', { width: 30, height: 30 })
  let base64 = canvas.toDataURL('image/jpeg', 0.3)

  axios.post('/my/user/avatar', `avatar=${encodeURIComponent(base64)}`).then(res => {
    let { status, message } = res.data
    if (status === 0) {
      layer.msg(message)
      window.parent.getUserInfo()
    }
  })
})